package com.qiwenshare.file.file_encryption;

/**
 * @author 成琪
 * @version 1.0
 * @date 2022-01-05 21:54
 * 工具类
 */
public class Utils {

/*    //测试
    public static void main(String[] args) {
        System.out.println(Utils.getFileName("f:\\test\\qwe.txt"));
    }*/

    /**
     * 从绝对路径中获取文件名
     *
     * @param path 绝对路径
     * @return 文件完整名
     */
    public static String getFileName(String path) {
        String[] s = new String[2];
        char[] cha = path.toCharArray();
        int num = cha.length - 1;
        for (int i = num; i >= 0; i--) {
            if ('\\' == cha[i]) {
                num = i + 1;
                break;
            }
        }
        return String.valueOf(cha, num, cha.length - num);
    }

    /**
     * Byte数组ASCII码转变字符串
     *
     * @param array Byte数组
     * @return 转换后的字符串
     */
    public static String getString(byte[] array) {
        char[] s = new char[array.length];
        for (int i = 0; i < array.length; i++) {
            s[i] = (char) array[i];
        }
        return String.valueOf(s);
    }


    /**
     * 字节数组转16进制
     *
     * @param bytes 需要转换的byte数组
     * @return 转换后的Hex字符串
     */
    public static String bytesToHex(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() < 2) {
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }


    public static String transform(String path){
        char[] cha = path.toCharArray();
        //System.out.println("path="+path);
        int num = cha.length;
        for (int i = 0; i <num; i++) {
            if ('/' == cha[i]) {
                cha[i]='\\';
            }
        }
        String string =String.valueOf(cha);
        //System.out.println("ok path = "+string);
        return string;
    }

}
